// Vue-router配置
import Vue from 'vue'
// 引入进度条插件
import NProgress from '@/plugins/nprogress/nproress.js'
// 引入、并使用vue-router插件
import Router from 'vue-router'
Vue.use(Router)
// 引入Vuex插件
import store from '../vuex/index'

//#region 一级路由开始
// 登录页
import LoginView from '@/views/login/LoginView'
// 主页
import HomeView from '@/views/home/HomeView'
// 页面不存在提示页
import NoSuchPage from '@/views/not-found/NoSuchPage'
//#endregion 一级路由结束

// #region 二级路由开始
// 系统管理
import UserManagementView from '@/views/business/system-management/UserManagementView'
import RoleManagementView from '@/views/business/system-management/RoleManagementView'
import NavigationManagementView from '@/views/business/system-management/NavigationManagementView'
import InterfaceManagementView from '@/views/business/system-management/InterfaceManagementView'
import IconFontManagementView from '@/views/business/system-management/IconFontManagementView'

// 文件管理
import UploadImage from '@/views/business/file-management/UploadImage'
import UploadVedio from '@/views/business/file-management/UploadVedio'
import UploadAudio from '@/views/business/file-management/UploadAudio'
import ServerPhysicFile from '@/views/business/file-management/ServerPhysicFile'

// 默认页 
import DefaultView from '@/views/home/default/DefaultView'
// 工作台
import DashBoard from '@/views/business/other/dashboard/DashBoard'

// echarts图表
import BarChart from '@/views/business/other/chart/BarChart'
import RadarChart from '@/views/business/other/chart/RadarChart'
import LineChart from '@/views/business/other/chart/LineChart'
import PineChart from '@/views/business/other/chart/PineChart'
import CandleStickChart from '@/views/business/other/chart/CandleStickChart'
import ThemeRiverChart from '@/views/business/other/chart/ThemeRiverChart'

// 富文本
import RichTextView from '@/views/business/other/rich-text/RichTextView'
// #endregion 二级路由结束

const router = new Router({
  // 路由详细配置
  routes: [
    // 一级路由配置

    // 1、根路径重定向至登录页
    {
      path: '/',
      redirect: '/login'
    },

    // 2、登录页
    {
      path: '/login',
      component: LoginView,
      meta: {
        title: '登录'
      }
    },
    // 3、主页，默认重定向至其中一个子菜单的路由
    {
      path: '/home',
      component: HomeView,
      redirect: '/default_page',
      // 子路由
      children: [
        {
          path: '/default_page',
          component: DefaultView,
          // 路由元信息
          meta: {
            // 标题
            title: '首页',
            // 是否允许展示到tab栏
            isTabable: true
          }
        },
        {
          path: '/user_management',
          component: UserManagementView,
          meta: {
            title: '用户管理',
            isTabable: true
          }
        },
        {
          path: '/role_management',
          component: RoleManagementView,
          meta: {
            title: '角色管理',
            isTabable: true
          }
        },
        {
          path: '/navigation_management',
          component: NavigationManagementView,
          meta: {
            title: '导航管理',
            isTabable: true
          }
        },
        {
          path: '/interface_management',
          component: InterfaceManagementView,
          meta: {
            title: '接口管理',
            isTabable: true
          }
        },
        {
          path: '/iconfont_management',
          component: IconFontManagementView,
          meta: {
            title: '字体图标管理',
            isTabable: true
          }
        },
        {
          path: '/server_physic_file',
          component: ServerPhysicFile,
          meta: {
            title: '服务器物理文件',
            isTabable: true
          }
        },
        {
          path: '/dashboard',
          component: DashBoard,
          meta: {
            title: '工作台',
            isTabable: true
          }
        },
        {
          path: '/upload_image',
          component: UploadImage,
          meta: {
            title: '上传图片',
            isTabable: true
          }
        },
        {
          path: '/upload_audio',
          component: UploadAudio,
          meta: {
            title: '上传音频',
            isTabable: true
          }
        },
        {
          path: '/upload_vedio',
          component: UploadVedio,
          meta: {
            title: '上传视频',
            isTabable: true
          }
        },
        {
          path: '/richtext',
          component: RichTextView,
          meta: {
            title: '富文本一',
            isTabable: true
          }
        },
        {
          path: '/bar_chart',
          component: BarChart,
          meta: {
            title: '柱状图',
            isTabable: true
          }
        },
        {
          path: '/radar_chart',
          component: RadarChart,
          meta: {
            title: '雷达图',
            isTabable: true
          }
        },
        {
          path: '/line_chart',
          component: LineChart,
          meta: {
            title: '堆叠折线图',
            isTabable: true
          }
        },
        {
          path: '/pine_chart',
          component: PineChart,
          meta: {
            title: '饼状图',
            isTabable: true
          }
        },
        {
          path: '/candle_stick_chart',
          component: CandleStickChart,
          meta: {
            title: 'K线图',
            isTabable: true
          }
        },
        {
          path: '/theme_river_chart',
          component: ThemeRiverChart,
          meta: {
            title: '主题河流图',
            isTabable: true
          }
        },
      ]
    },
    // 页面不存在提示页面(一级路由)
    {
      path: '/no_such_page',
      component: NoSuchPage,
      meta: {
        title: '未知页面'
      }
    },
    // 匹配不上已有路由的页面,重定向至页面不存在提示页面
    {
      path: '/*',
      redirect: '/no_such_page'
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // 如果路由到当前组件，强制不跳转
  if (to.path === from.path) {
    return next(false)
  }
  // 开启进度
  NProgress.start()
  // 如果跳转的是登录页，放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没携带token，则强制跳转到登录页
  if (!tokenStr)
    return next('/login')
  // 其它情况一律放行
  return next()
})

router.afterEach((to, from) => {
  if (to.path === from.path) console.log('重复路由')
  // 更新文档标题
  document.title = to.meta.title ? to.meta.title + ' | vue_auth_web' : 'vue_auth_web'
  // 路由对象
  let routeObj = {
    path: to.path,
    title: to.meta.title
  }
  // 更新Vuex里的当前活跃路径、当前菜单
  store.state.navigationModule.activePath = to.path
  store.state.navigationModule.currentRoute = routeObj
  // 关闭进度
  NProgress.done()
  // 如果允许在tab栏展示 
  if (to.meta.isTabable === true) {
    // 假设路由不存在
    let routeAlreadyExsit = false
    store.state.navigationModule.historyRouteList.forEach((item) => {
      if (item.path === to.path) {
        routeAlreadyExsit = true
      }
    })
    // 如果历史路由记录中没有当前路由信息，就将其添加至集合
    if (!routeAlreadyExsit) {
      store.state.navigationModule.historyRouteList.push(routeObj)
    }
  }
})

export default router
